package com.tanhan.mindapp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage; // 添加此行导入
import com.tanhan.mindapp.dao.Result;
import com.tanhan.mindapp.dao.SysMenu;
import com.tanhan.mindapp.dao.SysRole;
import com.tanhan.mindapp.dao.SysRoleDTO.SysRoleQueryDTO;
import com.tanhan.mindapp.service.SysRoleMenuService;
import com.tanhan.mindapp.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/sysRole")
@Tag(name = "系统角色管理", description = "管理系统中角色信息的增删改查操作")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @GetMapping("/findAll")
    @Operation(summary = "查询所有角色", description = "获取系统中所有角色的列表信息")
    public Result<List<SysRole>> list() {
        List<SysRole> list = sysRoleService.list();
        return Result.success(list);
    }

    @GetMapping("/findById/{id}")
    @Operation(summary = "根据ID查询角色", description = "根据角色唯一标识符查询单个角色的详细信息")
    public Result<SysRole> getById(@PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        return Result.success(role);
    }

    @PostMapping("/add")
    @Operation(summary = "新增角色", description = "创建一个新的角色信息")
    public Result<Void> save(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.save(sysRole);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "新增角色失败");
        }
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色", description = "根据角色ID更新角色信息")
    public Result<Void> update(@RequestBody SysRole sysRole) {
        boolean isSuccess = sysRoleService.updateById(sysRole);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "更新角色失败");
        }
    }

    /**
     * 分页查询角色列表
     *
     * @param sysRoleQueryDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询角色", description = "根据条件分页查询角色列表")
    public Result<IPage<SysRole>> page(@RequestBody SysRoleQueryDTO sysRoleQueryDTO) {
        IPage<SysRole> pageResult = sysRoleService.selectRolePage(sysRoleQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除角色", description = "根据角色ID删除指定角色")
    public Result<Void> delete(@PathVariable Long id) {
        boolean isSuccess = sysRoleService.deleteRole(id);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "删除角色失败");
        }
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "批量删除角色", description = "根据角色ID列表批量删除角色")
    public Result<Void> batchDelete(@RequestBody List<Long> ids) {
        boolean isSuccess = sysRoleService.deleteBatchRoles(ids);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "批量删除角色失败");
        }
    }

    @PostMapping("/assignMenus")
    @Operation(summary = "为角色分配菜单权限", description = "为指定角色分配菜单权限")
    public Result<Void> assignMenus(@RequestParam String roleId, @RequestBody List<String> menuIds) {
        boolean isSuccess = sysRoleService.assignMenuPermissions(roleId, menuIds);
        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error(9999, "分配菜单权限失败");
        }
    }

    @GetMapping("/getMenusByRoleId/{roleId}")
    @Operation(summary = "根据角色ID获取菜单ID列表", description = "根据角色ID获取该角色拥有的所有菜单ID列表")
    public Result<List<SysMenu>> getMenusByRoleId(@PathVariable String roleId) {
        List<SysMenu> menuIds = sysRoleService.getMenuList(roleId);
        return Result.success(menuIds);
    }

    @GetMapping("/menuPermissions/{roleId}")
    @Operation(summary = "获取角色菜单权限", description = "根据角色ID获取该角色拥有的所有菜单权限")
    public Result<List<String>> getMenuPermissions(@PathVariable String roleId) {
        List<String> menuIds = sysRoleService.getMenuPermissions(roleId);
        return Result.success(menuIds);
    }
}
